package com.zzzzzz.content.userfeel;

import javax.annotation.Resource;

import org.springframework.dao.DuplicateKeyException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.zzzzzz.core.dao.BaseDao;

@Repository
public class UserFeelDao extends BaseDao {
	@Resource
	private JdbcTemplate jdbcTemplate;
	
	private final static String ADD =			"insert into userfeel(id, id_type, user_id,feel_type) values(?, ?, ?, ?)";
	private final static String DEL = 			"delete from userfeel where id = ? and id_type = ? and user_id = ? and feel_type= ?";
	private final static String FIND_FEELTYPE = "select feel_type from userfeel where id = ? and id_type = ? and user_id = ?";
	
	public void add(UserFeel userFeel) throws UserFeelAlreadyExistsException{
		try{
			jdbcTemplate.update(ADD, userFeel.getId(), userFeel.getIdType(), userFeel.getUserId(), userFeel.getFeelType());
		}catch(DuplicateKeyException e){
			throw new UserFeelAlreadyExistsException();
		}
	}
	
	public void del(UserFeel userFeel){
		jdbcTemplate.update(DEL, userFeel.getId(), userFeel.getIdType(), userFeel.getUserId(), userFeel.getFeelType());
	}
	
	public Integer findFeelType(Long id, Integer idType, Long userId) throws NoSuchUserFeelException{
		try{
			return jdbcTemplate.queryForInt(FIND_FEELTYPE, id, idType, userId);
		} catch (EmptyResultDataAccessException e) {
			throw new NoSuchUserFeelException();
		}
		
	}
}
